package sharping.appilog.fragment;

import sharping.appilog.R;
import sharping.appilog.SignUp;
import sharping.appilog.Logic.Check_Value;
import sharping.appilog.Logic.Device_funtions;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class F_SignUp_Page4 extends Fragment {

	EditText et_email;
	ImageView im_next, im_back;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.signup_email, null);
		et_email = (EditText) v.findViewById(R.id.et_signup4);
		im_next = (ImageView) v.findViewById(R.id.signup4_next);
		im_back = (ImageView) v.findViewById(R.id.signup4_back);

		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setEvent();
	}

	private void setEvent() {
		et_email.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (Check_Value.isEmailAddress(s.toString())) {
					im_next.setVisibility(View.VISIBLE);
				} else {
					im_next.setVisibility(View.INVISIBLE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		
		et_email.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					if (Check_Value.isEmailAddress(v.getText().toString())) {
						onEditFinish();
					}
				}
				return false;
			}
		});

		im_next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onEditFinish();
			}

		});
		
		im_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onEditBack();
			}
		});
	}

	private void onEditFinish() {
		SignUp.userid = et_email.getText().toString();
		SignUp.pageChange(4, getFragmentManager());
		Device_funtions.hideKeyboard(getActivity(), et_email);
	}
	
	private void onEditBack() {
		SignUp.pageChange(2, getFragmentManager());
	}
}
